﻿using System.Collections;
using com.guojin.core.io.message;
using com.guojin.mj.net.message;
using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using UnityEngine;
using System.Collections.Generic;
using com.guojin.mj.net;

public class MessageManager : Observer
{
    private static MessageManager instance;
    public static MessageManager GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    void Start()
    {
        initMsg();
    }

    protected override string[] GetMsgList()
    {
        return new[]
        {
            MessageFactoryImpi.instance.getMessageString(7, 9), //登录错误
            MessageFactoryImpi.instance.getMessageString(7, 10), //登录成功
            MessageFactoryImpi.instance.getMessageString(7, 15), //pang 
            MessageFactoryImpi.instance.getMessageString(7, 16), //广播
            MessageFactoryImpi.instance.getMessageString(7, 17), //重复登录
            //MessageFactoryImpi.instance.getMessageString(7,36),   //俱乐部信息

            MessageFactoryImpi.instance.getMessageString(7, 1), //创建房间返回          
            MessageFactoryImpi.instance.getMessageString(7, 3), //解散房间
            MessageFactoryImpi.instance.getMessageString(7, 5), //退出房间
            MessageFactoryImpi.instance.getMessageString(7, 7), //加入房间返回
            MessageFactoryImpi.instance.getMessageString(7, 11), //创建，加入异常原因
            MessageFactoryImpi.instance.getMessageString(7, 13), //房卡变动
            MessageFactoryImpi.instance.getMessageString(7, 20), //战绩返回
            MessageFactoryImpi.instance.getMessageString(7, 23), //战绩详情返回
            //MessageFactoryImpi.instance.getMessageString(7, 28), //接收微信支付返回消息
            MessageFactoryImpi.instance.getMessageString(7, 30), //返回支付和客服微信号
            //MessageFactoryImpi.instance.getMessageString(7, 34), //俱乐部返回
            //MessageFactoryImpi.instance.getMessageString(7, 36), //俱乐部信息返回
            //MessageFactoryImpi.instance.getMessageString(7, 49), //俱乐部玩家状态
            //MessageFactoryImpi.instance.getMessageString(7, 52), //俱乐部邀请玩家返回
            //MessageFactoryImpi.instance.getMessageString(7, 55), //俱乐部获取玩家列表
            //MessageFactoryImpi.instance.getMessageString(7, 53), //俱乐部房卡变更
            MessageFactoryImpi.instance.getMessageString(7, 57), //创建房间规则返回
            //MessageFactoryImpi.instance.getMessageString(7, 38),  //代开房
            //MessageFactoryImpi.instance.getMessageString(7, 41),  //大转盘
            //MessageFactoryImpi.instance.getMessageString(7, 43),  //大转盘
            //MessageFactoryImpi.instance.getMessageString(7, 45),  //每日活动
            //MessageFactoryImpi.instance.getMessageString(7, 47),  //每日活动

            MessageFactoryImpi.instance.getMessageString(12, 2), //收到某个玩家准备消息
            MessageFactoryImpi.instance.getMessageString(12, 3), //收到开始发牌消息
            MessageFactoryImpi.instance.getMessageString(12, 7), //scene端加入返回房间信息
            MessageFactoryImpi.instance.getMessageString(12, 8), //其他玩家加入信息
            MessageFactoryImpi.instance.getMessageString(12, 9),   //每局结算信息
            MessageFactoryImpi.instance.getMessageString(12, 11), //下注申请返回
            MessageFactoryImpi.instance.getMessageString(12, 13), //下注详情返回所有人
            MessageFactoryImpi.instance.getMessageString(12, 15), //玩家托管消息
            MessageFactoryImpi.instance.getMessageString(12, 17),  //托管准备
            //MessageFactoryImpi.instance.getMessageString(12, 18), //总结算面板
            MessageFactoryImpi.instance.getMessageString(12, 19), //玩家掉线消息
            MessageFactoryImpi.instance.getMessageString(12, 21), //提交投票申请后全局显示投票面板
            MessageFactoryImpi.instance.getMessageString(12, 23), //全局返回投票结果
            MessageFactoryImpi.instance.getMessageString(12,25)
        };
    }

    public override void OnMsg(string msg, Message data)
    {

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 9))
            LoginController.GetInstance().OnReceiveMsg79(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 10))
            LoginController.GetInstance().OnReceiveMsg710(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 15))
            WorldTime.Instance.ReceiveMsg715(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 16))
            GameHallController.GetInstance().OnReceviceMsg716(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 17))
            LoginController.GetInstance().OnReceiveMsg717(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 1))
            CreateRoomController.GetInstance().OnReceiveMsg71(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 3))
            GameRoomController.GetInstance().OnReceiveMsg73(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 5))
            GameRoomController.GetInstance().OnReceiveMsg75(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 7))
            JoinRoomController.GetInstance().OnReceiveMsg77(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 11))
            NoticeController.GetInstance().OnReceiveMsg711(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 13))
            RoomCard.GetInstance().OnReceiveMsg713(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 20))
            WarRecordPage.GetInstance().OnReceive720(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 23))
            WarDetailsPage.GetInstance().OnReceive723(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 30))
            GameHallController.GetInstance().OnReceviceMsg730(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(7, 57))
            CreateRoomController.GetInstance().OnReceiveMsg757(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 2))
            GameController.GetInstance().OnReceive122(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 3))
            GameController.GetInstance().OnRecive123(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 7))
            GameRoomController.GetInstance().OnReceiveMsg127(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 8))
            GameController.GetInstance().OnReceive128(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 9))
           ChapterEndController.GetInstance().OnReceviceMsg129(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 11))
            BetPageController.GetInstance().OnReceiveMsg1211(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 13))
            GameController.GetInstance().OnReceiveMsg1213(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 15))
            GameController.GetInstance().OnReceive1215(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 17))
            GameController.GetInstance().OnReceive1217(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 19))
            GameController.GetInstance().OnReceive1219(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 21))
            GameController.GetInstance().OnReceive1221(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 23))
            GameController.GetInstance().OnReceive1223(msg, data);

        if (msg == MessageFactoryImpi.instance.getMessageString(12, 25))
        {
            MicReciveMessagePage.GetInstance().OnReceiveMsg1225(msg, data);
            ChatReciveMessagePage.GetInstance().OnReceiveMsg1225(msg, data);
        }

        #region

        //微信支付返回支付网页地址
        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 28))
        //{
        //    var wxpr = (SHWXPayRet) data;
        //    Debug.Log(wxpr.Reault);
        //    if (wxpr.Reault)
        //        LoginAndShare.Controller.OnRecePayRet(wxpr.Appid, wxpr.Noncestr, wxpr.Package, wxpr.Partnerid,
        //            wxpr.Prepayid, wxpr.Timestamp, wxpr.Sign);
        //    else
        //        Debug.Log("微信支付失败");
        //}

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 34))
        //    ClubReqPage.GetInstance().OnReceive734(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 36))
        //{
        //    var cir = (ClubInfoRet) data;
        //    if ((cir.JoinClub == 0) || (cir.JoinClub == 2))
        //    {
        //        UIManager.ChangeUI(UIManager.PageState.SHClubPage,
        //            obj => { obj.GetComponent<ClubReqPage>().InformationSetting(); });

        //        ClubReqPage.GetInstance().OnReceive736(msg, data);
        //    }
        //    else if (cir.JoinClub == 1)
        //    {
        //        if (GameObject.Find("ClubInfoPage(Clone)"))
        //        {
        //            ClubInfoPage.GetInstance().Refresh(msg, data);
        //        }
        //        else
        //        {
        //            UIManager.ChangeUI(UIManager.PageState.SHClubInfoPage,
        //                obj => { obj.GetComponent<ClubInfoPage>().InformationSetting(); });

        //            ClubInfoPage.GetInstance().OnReceive736(msg, data);
        //        }
        //    }
        //}

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 49))
        //    ClubInfoPage.GetInstance().OnReceive749(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 52))
        //{
        //    var cpr = (ClubInvitPLayerJoinGameRet) data;

        //    if (GameObject.Find("GameRoomPage(Clone)"))
        //    {
        //        Debug.Log("当前玩家在游戏中不接受邀请");
        //    }
        //    else
        //    {
        //        UIManager.ChangeUI(UIManager.PageState.SHClubInvitPage,
        //            obj => { obj.GetComponent<ClubBeInvitedPlayerJoinGamePage>().InformationSetting(); });

        //        ClubBeInvitedPlayerJoinGamePage.GetInstance().OnReceive752(msg, data);
        //    }
        //}

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 53))
        //    if (GameObject.Find("ClubInfoPage(Clone)"))
        //        ClubInfoPage.GetInstance().OnReceiveMsg753(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 55))
        //    ClubInvitePlayerJoinGamePage.GetInstance().OnReceive755(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 38))
        //    ReplaceCreatRoomPage.GetInstance().OnReceive738(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 41))
        //    LuckyRollerPage.GetInstance().OnReceive741(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 43))
        //    LuckyRollerPage.GetInstance().OnReceive743(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 45))
        //    ActivityPage.GetInstance().OnReceive745(msg, data);

        //if (msg == MessageFactoryImpi.instance.getMessageString(7, 47))
        //    ActivityPage.GetInstance().OnReceive747(msg, data);

        #endregion
    }

    public void SendMsg70(int roomType, List<OptionEntry> options)
    {
        var cr = CreateRoomReq.create(roomType, options);
        SocketMgr.GetInstance().Send(Net.instance.write(cr));
    }

    public void SendMsg76(string roomID)
    {
        var jr = JoinRoomReq.create(roomID);
        SocketMgr.GetInstance().Send(Net.instance.write(jr));
    }

    public void SendMsg78(string type, string token, string wxCode, string longitude, string latitude)
    {
        LoginReq log = LoginReq.create(type, token, wxCode, longitude, latitude);
        SocketMgr.GetInstance().Send(Net.instance.write(log));
    }

    public void SendMsg714(string str)
    {
        SHPing ping = SHPing.create(str);
        SocketMgr.GetInstance().Send(Net.instance.write(ping));
    }

    public void SendMsg719(string id)
    {
        WarRecordListReq wr = WarRecordListReq.Creat(id);
        SocketMgr.GetInstance().Send(Net.instance.write(wr));
    }

    public void SendMsg722(string authCode)
    {
        WarDetailsListReq ac = WarDetailsListReq.Create(authCode);
        SocketMgr.GetInstance().Send(Net.instance.write(ac));
    }

    public void SendMsg729()
    {
        ServiceWXReq sr = new ServiceWXReq();
        SocketMgr.GetInstance().Send(Net.instance.write(sr));
    }

    public void SendMsg732()
    {
        WeiXinShareReq weiXinShareReq = new WeiXinShareReq();
        SocketMgr.GetInstance().Send(Net.instance.write(weiXinShareReq));
    }

    public void SendMsg756()
    {
        CreateRoomPageRulesReq crprr = new CreateRoomPageRulesReq();
        SocketMgr.GetInstance().Send(Net.instance.write(crprr));
    }

    public void SendMsg121()
    {
        ReadyToStartTheGameReq gs = ReadyToStartTheGameReq.Creat(1, GameData.GetInstance().locationIndex);
        SocketMgr.GetInstance().Send(Net.instance.write(gs));
    }

    public void SendMsg126()
    {
        var Gjr = new GameRoomInfoReq();
        SocketMgr.GetInstance().Send(Net.instance.write(Gjr));
    }

    public void SendMsg1212(int betType, int locationIndex, int betOrCallScore)
    {
        var bir = BetInfoReq.Creat(betType, locationIndex, betOrCallScore);
        SocketMgr.GetInstance().Send(Net.instance.write(bir));
    }

    public void SendMsg1216() //0取消托管  1申请托管（目前没有）
    {
        UserTrusteeshipReq utr = UserTrusteeshipReq.create(GameData.GetInstance().locationIndex, 0);
        SocketMgr.GetInstance().Send(Net.instance.write(utr));
    }

    public void SendMsg1220()
    {
        VoteDissolveRoomReq voteDissolveRoom = new VoteDissolveRoomReq();
        SocketMgr.GetInstance().Send(Net.instance.write(voteDissolveRoom));
    }

    public void SendMsg1222(bool voteResult, int voteUserId)
    {
        VoteDissolveRoomResultReq uvr = VoteDissolveRoomResultReq.create(voteResult, voteUserId);
        SocketMgr.GetInstance().Send(com.guojin.mj.net.Net.instance.write(uvr));
    }

    public void SendMsg1224(string ID, int msgType)
    {
        PlayerSendChatInfo req = PlayerSendChatInfo.create(ID, msgType);
        SocketMgr.GetInstance().Send(com.guojin.mj.net.Net.instance.write(req));
    }

    public void SendMsg1229()
    {
        ExitRoomReq erq = new ExitRoomReq();
        SocketMgr.GetInstance().Send(Net.instance.write(erq));
    }

    public void SendMsg135(int _locationIndex, string _longitude, string _latitude)                  //向服务器发送经纬度
    {
        ReqLocation req = ReqLocation.Creat(0, _longitude, _latitude);
        SocketMgr.GetInstance().Send(Net.instance.write(req));
    }
}